package com.testing.login;

import com.testing.module.Result;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONObject;
import com.testing.mysql.MysqlUtils;


/**
 * @Classname ${NAME}
 * @Description 类型说明
 * @Date 2021/9/7 21:53
 * @Created by 特斯汀Roy
 */
@WebServlet(name = "Login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //修改返回字符集
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        //获取本次请求的session信息
        HttpSession session = request.getSession();
        //获取每次会话独一无二的sessionid信息。
        System.out.println(session.getId());
        Result result = new Result();
        String user = request.getParameter("user");
        String password = request.getParameter("password");

        //从session中获取一下user信息，如果是已经登录过的用户，是有user信息的
        //如果是没有登录过的用户，是没有user信息的。
        if (session.getAttribute("username") == null) {
            if (user != null && password != null) {
                //验证是否包含特殊字符，确定是否查询数据库
                Pattern spWord = Pattern.compile("[^\\w_\\-]");
                Matcher uM = spWord.matcher(user);
                Matcher pM = spWord.matcher(password);

                if (!uM.find() && !pM.find()) {
                    if (MysqlUtils.proCheckLogin(user, password)) {
                        result.setStatusCode("0");
                        result.setMsg("恭喜登录成功");
                        //如果登录成功了，服务端在session中记录一下用户名
                        session.setAttribute("username", user);
                        session.setAttribute("days", "5");
                        //设置session的有效周期
                        session.setMaxInactiveInterval(120);
//                        //并且返回cookie给客户端
                        Cookie cookie=new Cookie("JSESSIONID",session.getId());
                        //设置cookie的有效周期，和session保持一致
                        cookie.setMaxAge(120);
                        response.addCookie(cookie);
                    } else {
                        result.setStatusCode("-1");
                        result.setMsg("用户密码错误");
                    }
                } else {
                    result.setStatusCode("-3");
                    result.setMsg("用户名密码不允许包含特殊字符");
                }
            } else {
                result.setStatusCode("-2");
                result.setMsg("用户名密码必填");
            }
        } else {
            System.out.println(session.getAttribute("username"));
            result.setStatusCode("2");
            result.setMsg("已经有用户登录过了");
        }

        //将result对象转成json。借助一下fastjson库。
        String resultString = JSONObject.toJSONString(result);
        response.getWriter().append(resultString);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //修改返回字符集
        response.setContentType("text/plain;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        String user = request.getParameter("user");
        String password = request.getParameter("password");
        Result result = new Result();
        if (user != null && password != null) {
            if (MysqlUtils.checkLogin(user, password)) {
                result.setStatusCode("0");
                result.setMsg("恭喜登录成功");
            } else {
                result.setStatusCode("-1");
                result.setMsg("用户密码错误");
            }
        } else {
            result.setStatusCode("-2");
            result.setMsg("用户名密码必填");
        }
        //将result对象转成json。借助一下fastjson库。
        String resultString = JSONObject.toJSONString(result);
        response.getWriter().append(resultString);


    }
}
